/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entity.Curso;
import entity.Unidad;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Rodrigo
 */
@Stateless
public class UnidadFacade extends AbstractFacade<Unidad> {

    @PersistenceContext(unitName = "Epraxis-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UnidadFacade() {
        super(Unidad.class);
    }

    public int getOrdenSiguienteUnidad(Curso curso) {
        String sql = "SELECT MAX(U.ORDEN) FROM UNIDAD U "
                + "INNER JOIN CURSO C ON C.ID = U.CURSO "
                + "WHERE C.ID = " + curso.getId();

        Query query = em.createNativeQuery(sql);

        try {
            return (int) query.getSingleResult();
        } catch (Exception e) {
            return 1;
        }
    }

    public List<Unidad> getListaUnidadByCurso(Curso curso) {
        String sql = "SELECT * FROM UNIDAD WHERE CURSO = " + curso.getId();
        Query query = em.createNativeQuery(sql, Unidad.class);
        return query.getResultList();
    }

}
